
package com.idealighter.game.games.foursshop.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.dic.FoursShopRoomDic;
import com.idealighter.game.dictionary.domain.FoursShopRoomDomain;
import com.idealighter.game.games.foursshop.struct.FoursShopRoom;
import com.idealighter.game.games.foursshop.struct.FoursShopSeat;
import com.idealighter.game.games.foursshop.struct.FoursShopTable;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 4s店传数据管理.
 */
@Singleton
public class FoursShopDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(FoursShopDataMgr.class);

  private final Map<Long, FoursShopSeat> playerSeats = new HashMap<>();
  // 4s店游戏房间
  private final Map<Integer, FoursShopRoom> rooms;
  private FoursShopRoomDic roomDic;

  @Inject
  public FoursShopDataMgr(FoursShopRoomDic roomDic) {
    this.roomDic = roomDic;
    this.rooms = createRooms();
  }

  /**
   * 创建房间.
   * 
   * @return
   */
  private Map<Integer, FoursShopRoom> createRooms() {
    Map<Integer, FoursShopRoom> rooms = new LinkedHashMap<>();
    for (FoursShopRoomDomain roomDomain : roomDic.list()) {
      rooms.put(roomDomain.getId(), new FoursShopRoom(roomDomain));
      LOG.info("[4s]创建房间[{}][{}]", roomDomain.getId(), roomDomain.getName());
    }

    return rooms;
  }

  public Map<Long, FoursShopSeat> getPlayerSeats() {
    return playerSeats;
  }

  /**
   * 获取座位.
   * 
   * @param playerId 玩家id.
   * @return
   */
  public FoursShopSeat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 更新玩家座位.
   * 
   * @param playerId 玩家id.
   * @param seat 座位信息.
   */
  public void updatePlayerSeats(long playerId, FoursShopSeat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家seat.
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }

  /**
   * 获取房间.
   * 
   * @param roomId .
   * @return
   */
  public FoursShopRoom getRoom(int roomId) {

    return rooms.get(roomId);
  }

  /**
   * 获取所有房间集合.
   * 
   * @return
   */
  public Collection<FoursShopRoom> rooms() {

    return rooms.values();
  }

  /**
   * 获取牌桌.
   * 
   * @param tableId .
   * @return
   */
  public FoursShopTable getTable(int roomId, int tableId) {
    FoursShopRoom room = getRoom(roomId);

    if (room == null) {
      return null;
    } else {
      return room.tables.get(tableId);
    }
  }

}
